home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / plug-ins / perl / examples / ditherize < prev    next >
Encoding:
Text File  |  2000-05-21  |  2.0 KB  |  81 lines

  1. #!/usr/app/bin/perl
  2.  
  3. eval 'exec /usr/app/bin/perl  -S $0 ${1+"$@"}'
  4.     if 0; # not running under some shell
  5.  
  6. use strict 'subs';
  7. use Gimp;
  8. use Gimp::Fu;
  9.  
  10. #
  11. # this is quite convoluted, but I found no other way to do this than:
  12. #
  13. # create a new image & one layer
  14. # copy & paste the layer
  15. # ditherize new image
  16. # copy & paste back
  17. #
  18.  
  19. #Gimp::set_trace(TRACE_ALL);
  20.  
  21. my %imagetype2layertype = (
  22.    RGB,        RGB_IMAGE,
  23.    GRAY,    GRAY_IMAGE,
  24.    INDEXED,    INDEXED_IMAGE,
  25. );
  26.  
  27. register "plug_in_ditherize",
  28.          "dithers current selection",
  29.          "This script takes the current selection and dithers it just like convert to indexed",
  30.          "Marc Lehmann",
  31.          "Marc Lehmann",
  32.          "1.2",
  33.          N_"<Image>/Filters/Noise/Ditherize...",
  34.          "RGB*, GRAY*",
  35.          [
  36.           [PF_RADIO,        "dither_type",    "The dither type (see gimp_convert_indexed)", 1,
  37.                               [none => 0, fs => 1, "fs/low-bleed" => 2, ordered => 3]],
  38.           [PF_SLIDER,        "colours",    "The number of colours to dither to", 10, [0, 256, 1, 1]],
  39.          ],
  40.          sub {
  41.    my($image,$drawable,$dither,$colours)=@_;
  42.  
  43. #   Gimp::set_trace(-1);
  44.  
  45.    $drawable->is_layer or die "this plug-in only works for layers";
  46.  
  47.    $image->undo_push_group_start;
  48.  
  49.    # make sure something is selected
  50.    $drawable->mask_bounds or $image->selection_all;
  51.  
  52.    my ($x1,$y1,$x2,$y2)=($drawable->mask_bounds)[1..4];
  53.    my ($w,$h)=($x2-$x1,$y2-$y1);
  54.  
  55.    my $sel = $image->selection_save;
  56.    $image->rect_select($x1,$y1,$w,$h,REPLACE,0,0);
  57.    $drawable->edit_copy;
  58.    $sel->selection_load;
  59.    $sel->remove_channel;
  60.  
  61.    my $copy = new Image($w, $h, $image->base_type);
  62.    $copy->undo_disable;
  63.    my $draw = new Layer($copy, $w, $h,
  64.                         $imagetype2layertype{$image->base_type},
  65.                         "temporary layer", 100, NORMAL_MODE);
  66.    $copy->add_layer ($draw, 1);
  67.    $draw->edit_paste(0)->anchor;
  68.    $copy->convert_indexed ($dither, MAKE_PALETTE, $colours, 1, 1, "");
  69.  
  70.    $draw->edit_copy;
  71.    $drawable->edit_paste(1)->anchor;
  72.    $copy->delete;
  73.  
  74.    $image->undo_push_group_end;
  75.  
  76.    ();
  77. };
  78.  
  79. exit main;
  80.  
  81.